package com.jackrain.nea.omslocal.util;

/**
 * @Discription: Created with IntelliJ IDEA.
 * @Author cs
 * @Date 2017/10/10 17:44
 */
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.InstanceUtil;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class MyMQStartupRunner implements CommandLineRunner {
    private static final Logger log = LoggerFactory.getLogger(com.jackrain.nea.mq.MQStartupRunner.class);
    @Autowired
    private PropertiesConf pconf;
    @Autowired
    Environment env;
    private Map<Subscription, MessageListener> map = new HashMap();

    public MyMQStartupRunner() {
    }

    @Override
    public void run(String... args) throws Exception {
        Boolean producer = Boolean.valueOf(this.pconf.getProperty("ons.mq.producer.start"));
        Boolean consumer = Boolean.valueOf(this.pconf.getProperty("ons.mq.consumer.start"));
        if(producer.booleanValue()) {
            ProducerBean consumerBean = (ProducerBean)ApplicationContextHandle.getBean(ProducerBean.class);
            consumerBean.start();
        }

        if(consumer.booleanValue()) {
            this.initSubscription();
            ConsumerBean consumerBean1 = (ConsumerBean)ApplicationContextHandle.getBean("consumerBean");
            consumerBean1.setSubscriptionTable(this.map);
            consumerBean1.start();
        }

    }

    private void initSubscription() throws IllegalAccessException, InstantiationException {
        String dsPrefixs = this.pconf.getProperty("ons.mq.index");
        String logIntercept = this.pconf.getProperty("ons.mq.consumer.logIntercept");
        String[] var3 = dsPrefixs.split(",");
        int var4 = var3.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            String dsPrefix = var3[var5];
            String listenerC = this.pconf.getProperty("ons.mq.listenerC" + dsPrefix);
            String topic = this.pconf.getProperty("ons.mq.topic" + dsPrefix);
            String expr = this.pconf.getProperty("ons.mq.expr" + dsPrefix, "*");
            if(listenerC == null) {
                log.error("注意：Topic" + topic + "无监听程序");
                return;
            }

            Class classz = InstanceUtil.getClass(listenerC);
            if(logIntercept != null) {
                Class messageListener = InstanceUtil.getClass(logIntercept);
                Enhancer subscription = new Enhancer();
                subscription.setSuperclass(classz);
                subscription.setCallback((Callback)messageListener.newInstance());
                Object o = subscription.create();
                if(o instanceof MessageListener) {
                    MessageListener messageListener1 = (MessageListener)o;
                    Subscription subscription1 = new Subscription();
                    subscription1.setTopic(topic);
                    subscription1.setExpression(expr);
                    this.map.put(subscription1, messageListener1);
                } else {
                    log.error("MQ普通消息消费监听程序类型错误，参数ons.mq.topic" + dsPrefix);
                }
            } else {
                MessageListener var16 = (MessageListener)classz.newInstance();
                Subscription var17 = new Subscription();
                var17.setTopic(topic);
                var17.setExpression(expr);
                this.map.put(var17, var16);
            }
        }

    }
}

